﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NMock2;
using System.IO;

namespace ElectricBillMakerTest
{
    public class IsFloat
    {
        public static Matcher Near(float expected, float absError)
        {
            return new FloatNear(expected, absError);
        }
    }
    
    class FloatNear 
        : Matcher
    {
        private float expected_, absError_;
        public FloatNear(float expected, float absError)
        {
            expected_ = expected;
            absError_ = Math.Abs(absError);
        }

        public override void DescribeTo(TextWriter writer) 
        {
            writer.Write(expected_.ToString() + @";" + absError_.ToString());
        }

        public override bool Matches(object o)
        {
            if (!(o is float))
            {
                return false;
            }

            float f = (float)o;
            if (f == expected_)
            {
                return true;
            }

            if (Math.Abs(f - expected_) < absError_)
            {
                return true;
            }
            return false;
        }
    }
}
